const parameters = [{
  name: "售水参数",
  partnerUrl: "Manage/Equipment/getSaleWaterParam",
  partnerEditUrl: "Manage/Equipment/saleWaterParamEdit",
  type: "sale_water",
  list: [],
}, {
  name: "制水参数",
  partnerUrl: "Manage/Equipment/getWaterMakingParam",
  partnerEditUrl: "Manage/Equipment/waterMakingParamEdit",
  type: "system_water",
  list: [],
}, {
  name: "控制参数",
  partnerUrl: "Manage/Equipment/getControlParam",
  partnerEditUrl: "Manage/Equipment/controlParamEdit",
  type: "control",
  list: [],
}, {
  name: "系统参数",
  partnerUrl: "Manage/Equipment/getSystemParam",
  partnerEditUrl: "Manage/Equipment/systemParamEdit",
  type: "sys",
  list: [],
}, {
  name: "屏幕设置",
  partnerUrl: "Common/Equipment/getScreenParam",
  partnerEditUrl: "Common/Equipment/setScreenParam",
  type: "screen_param",
  list: [],
}];
let commonUrl = "Common/Equipment/getParam";
let commonEditUrl = "Common/Equipment/setParam";
const app = getApp();
const swiperTopRpx = 198;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameters,
    currentTab: 0,
    swiperTopPx: 0,
    havScreen:false
  },

  rpx2px(rpx) {
    return rpx / 750 * wx.getSystemInfoSync().windowWidth;
  },

  onPullDownRefresh: function () {
    const parameters = this.data.parameters;
    const tab = this.data.currentTab;
    this.getEquipmentParamter(parameters[tab], true)
  },

  onReady: function () {
    this.dateTime = this.selectComponent("#date")
  },

  showDate(e) {
    this.dateTime.show();
    this.dateIndex = e.currentTarget.dataset.index;
  },

  changeDate(e) {
    let value = e.detail.result;
    const tab = this.data.currentTab;
    this.setData({
      [`parameters[${tab}].list[${ this.dateIndex}].value`]: value
    })
  },
  //切换的时候就不需要调用了 防止多次重复调用
  chooseTab(e) {
    const value = e.currentTarget.dataset.index;
    this.setData({
      currentTab: value
    })
    if (parameters[value].list.length == 0) {
      //this.getEquipmentParamter(parameters[value], true)
    }
  },

  _formatTime(str) {
    if (str.length == 4) {
      return str.substr(0, 2) + ":" + str.substr(2);
    } else {
      return str;
    }
  },

  reduce(e) {
    const index = e.currentTarget.dataset.index;
    const tab = this.data.currentTab;
    let value = this.data.parameters[tab].list[index].value - 1;
    this.setData({
      [`parameters[${tab}].list[${index}].value`]: value < 0 ? 0 : value,
    })
  },

  plus(e) {
    const index = e.currentTarget.dataset.index;
    const tab = this.data.currentTab;
    let value = this.data.parameters[tab].list[index].value - 0 + 1;
    this.setData({
      [`parameters[${tab}].list[${index}].value`]: value,
    })
  },

  handleInput(e) {
    const value = e.detail.value;
    const tab = this.data.currentTab;
    const index = e.currentTarget.dataset.index;
    this.setData({
      [`parameters[${tab}].list[${index}].value`]: value,
    })
  },


  pickerTime(e) {
    const value = e.detail.value;
    const tab = this.data.currentTab;
    const index = e.currentTarget.dataset.index;
    this.setData({
      [`parameters[${tab}].list[${index}].value`]: value,
    })
  },

  chooseRadio(e) {
    const {
      oid,
      index
    } = e.currentTarget.dataset;
    const parameters = this.data.parameters;
    const tab = this.data.currentTab;
    let value;
    let option = parameters[tab].list[index].option;
    option.forEach((item, i) => {
      if (i == oid) {
        item.checked = true;
        value = item.value;
      } else {
        item.checked = false;
      }
    })
    this.setData({
      [`parameters[${tab}].list[${index}].value`]: value,
      [`parameters[${tab}].list[${index}].option`]: option,
    })

  },


  //这里点击切换的时候就会调用接口,所以在点击的时候就不需要调用了
  bindchange(e) {
    const value = e.detail.current;
    this.setData({
      currentTab: value
    })
    const parameters = this.data.parameters;
    if (parameters[value].list.length == 0) {
      this.getEquipmentParamter(parameters[value], true)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      swiperTopPx: this.rpx2px(swiperTopRpx)
    })
    if (options.name) {
      let index = this.data.parameters.findIndex(item => item.type == options.name);
      this.setData({
        currentTab: ~index ? index : 0
      })
    }
    this.sn = options.sn; //设备编号
    this.from = options.from; //来源 1设备列表  2来自设备调试
    let parameter = this.data.parameters[this.data.currentTab];
    let parameter2 = this.data.parameters[4];//默认请求一次接口 判断是否有屏幕设置
    this.getEquipmentParamter(parameter);
    this.getEquipmentParamter(parameter2,false,1);
  },




  //获取设备的参数信息 check=1需要验证是否有屏幕设置
  getEquipmentParamter(parameter, isLoading = false,check=0) {
    const param = {
      sn: this.sn
    }
    let url = parameter.partnerUrl;
    // 这里需要注意 设备调试都用的同一个接口,但是屏幕设置用了另外两个接口
    if (this.from == 2 && parameter.type!='screen_param') {
      param.type = parameter.type;
      url = commonUrl;
    }
    isLoading && wx.showLoading({
      mask: true,
    })
    app.ajax({
      url,
      data: param,
      success: res => {
        wx.stopPullDownRefresh();
        isLoading && wx.hideLoading()
        if (res.data.code == 1000 && check!=1) {
          const currentTab = this.data.currentTab;
          const source = res.data.data;
          source.forEach(current => {
            if (current.type == "time") {
              current.value = this._formatTime(current.value);
            }
          })
          this.setData({
            [`parameters[${currentTab}].list`]: source,
          })
        } else if(res.data.code == 1000 && check==1) {//有屏幕
            this.setData({havScreen:true})
        }else if(res.data.code == -1000 && check==1){//无屏幕
          this.setData({havScreen:false})
        }else{
          app.showToast(res.data.message);
        }
      },
      error: err => {
        wx.stopPullDownRefresh();
        isLoading && wx.hideLoading()
        app.showToast("网络错误")
      }
    })
  },

  emit(e) {
    const index = e.currentTarget.dataset.index;
    const tab = this.data.currentTab;
    const parameters = this.data.parameters;
    let listItem = parameters[tab].list[index];
    let key = listItem.id;
    let instruction = listItem.instruction;
    let val = listItem.value;
    if (listItem.type == "time") {
      val = val.replace(":", "")
    }
    let param ={};
    //屏幕需要单独处理
    if (parameters[tab].type == "screen_param") {
      param={
        sn:  this.sn,
        instruction: instruction,
        content: val,
      }
    }else{
      param={
        sn: this.sn,
        param: key,
        value: val
      }
    }
    console.log('listItem.type',listItem.type ,param);
    let url = parameters[tab].partnerEditUrl;
    // 这里需要注意 设备调试都用的同一个接口,但是屏幕设置用了另外两个接口
    if (this.from == 2 && parameters[tab].type!='screen_param') {
      url = commonEditUrl;
      Object.assign(param, {
        type: parameters[tab].type
      })
    }
    wx.showLoading({
      mask: true
    })
    app.ajax({
      url,
      data: param,
      success: res => {
        wx.hideLoading();
        if (res.data.code > 0) {
          app.showToast(res.data.message, "success");
        } else {
          app.showToast(res.data.message);
        }
      },
      error: err => {
        wx.hideLoading();
        app.showToast("网络错误");
      }
    })
  },
})